package elitech.vietnam.myfashion.view;

import java.util.Locale;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import elitech.vietnam.myfashion.LoginActivity;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.dialog.ChangePasswordDialog;
import elitech.vietnam.myfashion.dialog.EditShippingDialog;
import elitech.vietnam.myfashion.dialog.OkCancelWarnningDialog;
import elitech.vietnam.myfashion.entites.Order;
import elitech.vietnam.myfashion.extend.ViewPagerAdapter;
import elitech.vietnam.myfashion.fragment.EditProfileFragment;
import elitech.vietnam.myfashion.fragment.FollowListFragment;
import elitech.vietnam.myfashion.fragment.NotificationSettingFragment;
import elitech.vietnam.myfashion.fragment.NotificationsFragment;
import elitech.vietnam.myfashion.wsclient.Service;

public class SettingView extends LinearLayout implements OnClickListener {
	public static final int	LOGINACTIVITY	= 1;
	private MainActivity	mContext;
	private Button			mBtnLogin;
	private TextView		mTxtLanguage, mTxtVersion;
	private LinearLayout	mBtnEditProfile, mBtnFriendList, mBtnChangePassword, mBtnShippingAddress,
			mBtnGetNotification, mBtnLanguage, mBtnHomePage, mBtnNotifications;
	private ToggleButton	mToggle;
	private LinearLayout	mMainMenu;

	public SettingView(MainActivity context) {
		super(context);
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.setting, this);
		}

		mBtnLogin = (Button) findViewById(R.id.setting_login);
		mBtnEditProfile = (LinearLayout) findViewById(R.id.setting_btneditprofile);
		mBtnFriendList = (LinearLayout) findViewById(R.id.setting_btnfriendlist);
		mBtnChangePassword = (LinearLayout) findViewById(R.id.setting_btnchangepassword);
		mBtnShippingAddress = (LinearLayout) findViewById(R.id.setting_btnshippingaddress);
		mBtnGetNotification = (LinearLayout) findViewById(R.id.setting_btngetnotification);
		mToggle = (ToggleButton) findViewById(R.id.setting_togglenotification);
		mBtnLanguage = (LinearLayout) findViewById(R.id.setting_btnlanguage);
		mBtnHomePage = (LinearLayout) findViewById(R.id.setting_btnHomePage);
		mTxtLanguage = (TextView) findViewById(R.id.setting_txtlanguage);
		mBtnNotifications = (LinearLayout) findViewById(R.id.setting_btnnotifications);
		mTxtVersion = (TextView) findViewById(R.id.setting_txtVersion);
		mMainMenu = (LinearLayout) findViewById(R.id.main_menu);
		mMainMenu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mContext.callMenu();
			}
		});

		mBtnLogin.setOnClickListener(this);
		mBtnEditProfile.setOnClickListener(this);
		mBtnFriendList.setOnClickListener(this);
		mBtnChangePassword.setOnClickListener(this);
		mBtnShippingAddress.setOnClickListener(this);
		mBtnGetNotification.setOnClickListener(this);
		mBtnLanguage.setOnClickListener(this);
		mBtnHomePage.setOnClickListener(this);
		mBtnNotifications.setOnClickListener(this);
		mTxtLanguage.setText(mContext.getLocale().getDisplayLanguage());

		if (mContext.mUser != null)
			mBtnLogin.setText(mContext.getString(R.string.signout));
		mToggle.setChecked(mContext.GetPreferenceBool("GetNotification"));

		try {
			PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
			mTxtVersion.setText(pInfo.versionName);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void onResume() {
		if (mContext.mUser != null)
			mBtnLogin.setText(mContext.getString(R.string.signout));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_login:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				i.putExtra("ReturnToTab", 5);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				new OkCancelWarnningDialog(mContext, mContext.getString(R.string.logoutconfirm)) {
					public void onOk() {
						new AsyncTask<Integer, Integer, Boolean>() {
							ProgressDialog	mDialog	= new ProgressDialog(mContext);

							@Override
							protected void onPreExecute() {
								mDialog.setMessage(mContext.getString(R.string.loading));
								mDialog.show();
							}

							@Override
							protected Boolean doInBackground(Integer... params) {
								int rs = 0;
								try {
									rs = new Service().updateGCMID(mContext.mUser.Id, "");
								} catch (Exception e) {
								}
								return rs > 0;
							}

							@Override
							protected void onPostExecute(Boolean result) {
								mDialog.dismiss();
								if (result) {
									mContext.mOrder = new Order();
									mContext.mUser = null;
									mContext.AddPreference("User", "");
									mBtnLogin.setText(mContext.getString(R.string.login));
									mContext.mMyShoppingView = null;
									mContext.mMainPageView.mPageAdapter = new ViewPagerAdapter(mContext);
									mContext.mListProductBest.clear();
									mContext.mListMyStyles.clear();
									mContext.mMainPageView.mViewPager.setAdapter(mContext.mMainPageView.mPageAdapter);
									mContext.mMainPageView.mViewPager.setCurrentItem(3, false);
									mContext.AddPreferenceBool("LoggedIn", false);
									mContext.onProfileChanged();
									Toast.makeText(mContext, R.string.logoutsuccess, Toast.LENGTH_SHORT).show();
								} else
									Toast.makeText(mContext, R.string.logoutfailed, Toast.LENGTH_SHORT).show();
							}
						}.execute();
					};
				}.show();
			}
			break;

		case R.id.setting_btneditprofile:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				i.putExtra("ReturnToTab", 5);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				if (mContext.mEditProfileFragment == null)
					mContext.mEditProfileFragment = new EditProfileFragment();
				if (!mContext.mEditProfileFragment.isAdded()) {
					mContext.replaceFragment(mContext.mEditProfileFragment, true);
				}
			}
			break;
		case R.id.setting_btnnotifications:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				i.putExtra("ReturnToTab", 5);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				if (mContext.mNotiFragment == null)
					mContext.mNotiFragment = new NotificationsFragment();
				if (!mContext.mNotiFragment.isAdded()) {
					mContext.replaceFragment(mContext.mNotiFragment, true);
				}
			}
			break;
		case R.id.setting_btnfriendlist:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				i.putExtra("ReturnToTab", 5);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				if (mContext.mFollowListFragment == null)
					mContext.mFollowListFragment = new FollowListFragment();
				if (!mContext.mFollowListFragment.isAdded()) {
					mContext.replaceFragment(mContext.mFollowListFragment, true);
				}
			}
			break;

		case R.id.setting_btnchangepassword:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				i.putExtra("ReturnToTab", 5);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				new ChangePasswordDialog(mContext).show();
			}
			break;

		case R.id.setting_btnshippingaddress:
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				i.putExtra("ReturnToTab", 5);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				new EditShippingDialog(mContext).show();
			}
			break;

		case R.id.setting_btnlanguage:
			new Dialog(mContext).show();
			break;

		case R.id.setting_btnHomePage:
			Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mContext.getString(R.string.homepageurl)));
			mContext.startActivity(browserIntent);
			break;

		case R.id.setting_btngetnotification:
			// mToggle.performClick();
			// mContext.AddPreferenceBool("GetNotification",
			// mToggle.isChecked());
			if (mContext.mUser == null) {
				Intent i = new Intent(mContext, LoginActivity.class);
				i.putExtra("ReturnToTab", 5);
				mContext.startActivityForResult(i, LOGINACTIVITY);
				break;
			} else {
				if (mContext.mNotiSettingFragment == null)
					mContext.mNotiSettingFragment = new NotificationSettingFragment();
				if (!mContext.mNotiSettingFragment.isAdded()) {
					mContext.replaceFragment(mContext.mNotiSettingFragment, true);
				}
			}
			break;
		}

	}

	public SettingView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SettingView(Context context) {
		super(context);
	}

	private class Dialog extends android.app.Dialog implements View.OnClickListener {
		public Dialog(Context context) {
			super(context, R.style.Dialog);
		}

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.dl_language);

			this.findViewById(R.id.dl_language_EN).setOnClickListener(this);
			this.findViewById(R.id.dl_language_VN).setOnClickListener(this);
			this.findViewById(R.id.dl_language_KR).setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			Intent i = new Intent(mContext, MainActivity.class);
			i.putExtra("ReusumeReturnToTab", mContext.mMainPageView.mViewPager.getCurrentItem());
			switch (v.getId()) {
			case R.id.dl_language_EN:
				setDefaultLocale(getContext().getApplicationContext(), Locale.ENGLISH);
				mContext.startActivity(i);
				mContext.finish();
				break;
			case R.id.dl_language_VN:
				setDefaultLocale(getContext().getApplicationContext(), new Locale("vi_VN"));
				mContext.startActivity(i);
				mContext.finish();
				break;
			case R.id.dl_language_KR:
				setDefaultLocale(getContext().getApplicationContext(), Locale.KOREA);
				mContext.startActivity(i);
				mContext.finish();
				break;
			}
			dismiss();
		}

		public void setDefaultLocale(Context context, Locale locale) {
			Locale.setDefault(locale);
			Configuration appConfig = new Configuration();
			appConfig.locale = locale;
			context.getResources().updateConfiguration(appConfig, context.getResources().getDisplayMetrics());
			System.out.println("trad" + locale.getLanguage());
		}

	}

}
